package com.niit.servlet;

import com.niit.pojo.StudentTable;
import com.niit.service.HbaseService;
import com.niit.service.impl.HbaseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;

/**
 * @author 杨铭
 * 2022/2/7,17:20
 */
@WebServlet("/addServlet")
public class addServlet extends HttpServlet {

    public void init(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
    }
    /**
     * 为请求包和相应包初始化
     **/
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentTable studentTable = new StudentTable(req.getParameter("studentId"), req.getParameter("name"),
                req.getParameter("dateOfEnrollment"), req.getParameter("contactNumber"),
                req.getParameter("semester"), "A");
//        System.out.println(studentTable.toString());
        HbaseService hbaseService = new HbaseServiceImpl();
        int judge = hbaseService.add(studentTable);
        init(req, resp);
        if (judge == 0) {
            req.getRequestDispatcher("/pages/add_true.jsp").forward(req, resp);
        } else {
            req.getRequestDispatcher("/pages/add_false.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }


}
